/*
  Copyright (c) 2006-2009 by HAO jing <haojingus@gmail.com>
  This file is part of the SVG library. http://libsvg.googlecode.com

  This software is distributed under a license. The full license
  agreement can be found in the file LICENSE in this distribution.
  This software may not be copied, modified, sold or distributed
  other than expressed in the named license agreement.

  This software is distributed without any warranty.
*/

#pragma warning(disable : 4996)

#ifndef SVGTAG_H__
#define SVGTAG_H__

#include "macros.h"
#include "gloox.h"
#include <string>
#include <string.h>
#include <stdlib.h>

namespace gloox{

SVG_API extern const std::string XMLNS_SVG_BASE;
SVG_API extern const std::string XMLNS_SVG_LINE;
SVG_API extern const std::string XMLNS_SVG_RECT;
SVG_API extern const std::string XMLNS_SVG_TEXT;
SVG_API extern const std::string XMLNS_SVG_POLYLINE;
SVG_API extern const std::string XMLNS_SVG_CIRCLE;
SVG_API extern const std::string XMLNS_SVG_DESC;

SVG_API typedef struct SvgStyle
{
	char class_name[32];
	char font_family[32];
	char font_size[32];
	char fill[16];
	char text_anchor[16];
	char stroke[16];
	char stroke_width[16];
	char stroke_dasharray[16];

} CSS;



SVG_API typedef struct svgPoint
{
	int x;
	int y;
} SVGPOINT;

static void str_trim(const char* source,char* dest,char sep)
{
	char c;
	char cLastChar	= sep;
	c	= *source;
	int i = 0;
	while(c!='\0')
	{
		//check from front
		if(c!=sep||c!=cLastChar)
		{
			cLastChar	= c;
			*dest	= c;
			dest++;
			i++;

		}
		source++;
		c = *source;
	}
	if(i)
	{
		if(*(--dest)==sep)
			*dest = '\0';

	}

}


static 	void split(const char* source, SVGPOINT* points, char* c)
{
	int iIn	= 0;

	//int _pointCount	= str_count(_buf,' ')+1;
	char *szBuf	=const_cast<char*>(source);
	str_trim(source,szBuf,',');
	//char *_ptr = new char[_pointCount];
	char *arrPtr[SVG_POLYGONPOINTS];
	char* szOutPtr	= NULL;
	char* szInPtr	= NULL;


	while((arrPtr[iIn]=strtok(szBuf,",",&szOutPtr))!=NULL)
	{
		szBuf	= arrPtr[iIn];

		while((arrPtr[iIn]=strtok(szBuf," ",&szInPtr))!=NULL)
		{

			iIn++;
			szBuf	= NULL;
		}


	}
	for(int j=0;j<iIn-1;j++)
	{

			SVGPOINT POINT_node	= {0,0};
			POINT_node.x	= atoi(arrPtr[j]);
			j++;
			POINT_node.y	= atoi(arrPtr[j]);
			*points	= POINT_node;
			points++;
	}
	//	printf("%s ",_ptr[0]);
}

static int  str_split(const char* source, char** dest,const char* sep1,const char* sep2)
{


	int i	= 0;

	//int _pointCount	= str_count(_buf,' ')+1;
	char *szBuf	=const_cast<char*>(source);
	str_trim(source,szBuf,',');
	//char *_ptr = new char[_pointCount];
	char *arrPtr[SVG_POLYGONPOINTS];
	char* szOutPtr	= NULL;
	char* szInPtr	= NULL;



	while((arrPtr[i]=strtok(szBuf,sep1,&szOutPtr))!=NULL)
	{
		szBuf	= arrPtr[i];
		while((arrPtr[i]=strtok(szBuf,sep2,&szInPtr))!=NULL)
		{
			i++;
			szBuf	= NULL;
		}


	}


	for(int j=0;j<i;j++)
	{
		*dest	= arrPtr[j];
		dest++;
	}
	return (i==0)?0:i/2;
}

static int str_count(const char* source,char dest)
{
	char c;
	c	= *source;
	int i=0;
	while(c!='\0')
	{
		if(c==dest)
			i++;
		source++;
		c = *source;
	}
	return i;
}

//
//static const char* svgStyleTypeKeys[] =
//{
//	"font_family", "font_size", "fill", "text_anchor", "stroke", "stroke_width", "stroke_dasharray"
//};


static void getCssAttribute(char** source,int size_s,CSS* css)
{
	
	for(int i=0;i<size_s;i++)
	{
		if(!strcmp(*source,"font_family"))
			strcpy(css->font_family,*(++source));
		else if(!strcmp(*source,"font_size"))
			strcpy(css->font_size,*(++source));
		else if(!strcmp(*source,"fill"))
			strcpy(css->fill,*(++source));
		else if(!strcmp(*source,"text_anchor"))
			strcpy(css->text_anchor,*(++source));
		else if(!strcmp(*source,"text_anchor"))
			strcpy(css->text_anchor,*(++source));
		else if(!strcmp(*source,"stroke"))
			strcpy(css->stroke,*(source));
		else if(!strcmp(*source,"stroke_width"))
			strcpy(css->stroke_width,*(++source));
		else if(!strcmp(*source,"stroke_dasharray"))
			strcpy(css->stroke_dasharray,*(++source));

		i++;

	}

}


}

#endif
